//
//  FZEmotionCollCell.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/4/7.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit

class FZEmotionCollCell: UICollectionViewCell {
    
    fileprivate lazy var emotionBtnArray : [FZEmotionButton] = [FZEmotionButton]()
    fileprivate lazy var deleteButton : UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named : "compose_emotion_delete"), for: .normal)
        btn.setImage(UIImage(named : "compose_emotion_delete_highlighted"), for: .highlighted)
        btn.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        return btn
    }()
    
    var emotionArray : [FZEmotionModel]? {
        didSet {
            
            guard let currentBtnArray = emotionArray else {
                return
            }
            
            for btn in emotionBtnArray {
                
                btn.isHidden = true
            }
            for (i , model) in currentBtnArray.enumerated() {
                
                let button = emotionBtnArray[i]
                button.isHidden = false
                button.emotionModel = model
                if model.type == "0" {
                   //图片表情
                    button.setImage(UIImage(named : model.allPath!), for: .normal)
                    button.setTitle(nil, for: .normal)
                    
                }else{
                  //emoji表情
                   let emoji = (model.code! as NSString).emoji()
                   button.setImage(nil, for: .normal)
                   button.setTitle(emoji, for: .normal)
                }
                
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    fileprivate func setupUI() {
      addSubview(deleteButton)
      addBtnsArray()
    
    }
    
    @objc func deleteButtonAction() {
        
        NotificationCenter.default.post(name: NSNotification.Name(deleteTextViewNoti), object: nil)
    }
    
    fileprivate func addBtnsArray() {
        
        for _ in 0 ..< 20 {
          let button = FZEmotionButton()
          button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
          button.addTarget(self, action: #selector(emotionAttributeStr(sender:)), for: .touchUpInside)
          contentView.addSubview(button)
          emotionBtnArray.append(button)
        }
    }
    
    @objc private func emotionAttributeStr(sender : FZEmotionButton) {
        
        NotificationCenter.default.post(name: Notification.Name(clickEmotionToTextViewNoti), object: sender.emotionModel)
        
    }
    
    override func layoutSubviews() {
        //每一项的宽度
        let emotionItemWidth = contentView.width / 7
        //每一项的宽度
        let emotionItemHeight = contentView.height / 3
        
        for (i, button) in emotionBtnArray.enumerated() {
            //列索引
          let col = i % 7
            //行索引
          let row = i / 7
          let x = CGFloat(col) * emotionItemWidth
          let y = CGFloat(row) * emotionItemHeight
          
          button.frame = CGRect(x: x, y: y, width: emotionItemWidth, height: emotionItemHeight)
        }
        deleteButton.frame = CGRect(x: contentView.width - emotionItemWidth, y: contentView.height - emotionItemHeight, width: emotionItemWidth, height: emotionItemHeight)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}






